
package eclipse.server;
import java.awt.*;
import java.io.*;
import eclipse.lib.scripting.*;
import eclipse.util.io.*;
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class ServerApplication
{
	/**
	 *  The Server Application 	 
	 */
	public static void main(String Args[]) throws Exception
	{		
		//process any runtime arguments
		System.out.println("Processing arguments...");	Loader.ArgsProcess(Args);
		//confirm that the environment is ready for the server
		System.out.println("Building Environment...");  Loader.buildEnvironment();
		//launch the TCP connections
		System.out.println("Loading TCP...");			Loader.TCPInit();
		//launch the thread database
		System.out.println("Loading Threads...");		Loader.ThreadInit();
		//start the Scripting engine
		System.out.println("Loading Scripts...");		Loader.ScriptInit();
		
		
		//run the startup script
		JScript.run("onServerLoad();");
		new ServerTray();
		//start taking input from the console
		inputLoop();
	}	
	/**
	 * The console input reader.
	 * This will check for input for the console and executes it as either native or script. 
	 * @throws IOException 
	 * @throws AWTException 
	 */
	public static final void inputLoop() throws IOException, AWTException
	{		
		Database.console.cls();
		Database.console.println("  _________________  .____       _____ __________ ", 36);
		Database.console.println(" /   _____/\\_____  \\ |    |     /  _  \\\\______   \\", 36);
		Database.console.println(" \\_____  \\  /   |   \\|    |    /  /_\\  \\|       _/", 36);
		Database.console.println(" /        \\/    |    \\    |___/    |    \\    |   \\", 36);
		Database.console.println("/_______  /\\_______  /_______ \\____|__  /____|_  /", 36);
		Database.console.println("        \\/         \\/        \\/       \\/       \\/", 36);
		Database.console.println("____________________ .____    ._____________  ____________________", 36);
		Database.console.println("\\_   _____/\\_   ___ \\|    |   |   \\______   \\/   _____/\\_   _____/", 36);
		Database.console.println(" |    __)_ /    \\  \\/|    |   |   ||     ___/\\_____  \\  |    __)_ ", 36);
		Database.console.println(" |        \\\\     \\___|    |___|   ||    |    /        \\ |        \\", 36);
		Database.console.println("/_______  / \\______  /_______ \\___||____|   /_______  //_______  /", 36);
		Database.console.println("        \\/         \\/        \\/                     \\/         \\/", 36);
		Database.console.println("___________________________", 32);
		Database.console.println("|VERSION:         1 .0 Beta|", 32);
		Database.console.println("|ARCH:             Genesis_|", 32);
		Database.console.println("|__________________________|", 32);		
		Database.console.print("STATUS: ", 30);
		Database.console.println("    ONLINE", 32);
		
		//determines whether or not the program is running
		boolean running = true;
		
		//only run while the program should run
		while(running)
		{
			//Attempt to read from the console
			try 
			{
				//Read only a line from the console. This will block
				Database.console.print("<< ", 34);
				String input = Database.console.readLine().replace("<< ", "");				
				
				//check whether the command should be interpreted with Native commands				
				if(input.startsWith("Native."))
				{					
					//remove the "Native." header because it really isnt used
					input = input.replace("Native.", "");
					//remove any special text that might have been used
					input = IOTools.strReplace(input, " ", "(", ")", ",", ";");
					//run the command
					Runtime.getRuntime().exec(input);
				}
				
				// the command is not native so use scripts
				else
				{
					//use JavaScript to execute the command
					JScript.run(input);
				}
			}
			//An error occurred for whatever reasons
			catch (Exception e) 
			{			
				//handle the error using the ErrorManager
				StackTraceElement[] stack = e.getStackTrace();
				//output that there was an error using red.
				Database.console.println("ERROR LIST", 31);
				//print all messages
				for(int i = 0; i < stack.length; i++)				
					Database.console.println("    " + stack[i].toString(), 31);			
			}
			
		}
	}
	
}
